package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.account.service.facade.model.request.RechargeReqDTO;
import com.baofu.paycore.biz.external.account.AccountService;
import com.baofu.paycore.biz.paycore.converter.BO2BOConverter;
import com.baofu.paycore.manager.model.AccountRespBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.biz.engine.command.impl.RechargeCommand;
import com.baofu.paycore.biz.engine.processor.BaseProcessor;
import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.system.commons.utils.BeanCopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账务充值处理器
 * <p>
 *  1、获取处理器类型
 *  2、执行特定支付工具的特定支付类型的批量支付命令，并返回相应的结果
 *  3、转账务充值参数
 * </p>
 * User: xin deng  Date: 16/4/28 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class AccRechargeProcessor implements BaseProcessor<RechargeCommand> {

    /**
     * 调用账务充值服务
     */
    @Autowired
    private AccountService accountService;

    /**
     * 获取处理器类型
     *
     * @return 命令处理器
     */
    @Override
    public CommandProcessorEnum getProcessorType() {
        return CommandProcessorEnum.ACC_RECHARGE_PROCESSOR;
    }

    /**
     * 执行特定支付工具的特定支付类型的批量支付命令，并返回相应的结果
     *
     * @param rechargeCommand 支付命令
     * @return 指令执行结果
     */
    @Override
    public Map<String, CommandResult> doProcess(RechargeCommand rechargeCommand) {

        log.debug("进入账务充值统一处理器：commands={}", rechargeCommand);
        Map<String, CommandResult> result = new HashMap<String, CommandResult>();

        try {
            // 指令转换成账务请求对象
            RechargeReqDTO rechargeReqDTO = BeanCopyUtil.objConvert(rechargeCommand, RechargeReqDTO.class);

            log.info("日志跟踪号:{} 调账务充值接口参数:{}",rechargeCommand.getTraceLogId(), rechargeReqDTO);
            AccountRespBO accountResult = accountService.recharge(rechargeCommand.getFundDetailNo(),
                    rechargeCommand.getServiceCode(), rechargeReqDTO);

            CommandResult payResult = new CommandResult();
            BO2BOConverter.buildCommandResult(payResult, accountResult);
            payResult.setCommand(rechargeCommand);
            result.put(rechargeCommand.getCommandNo(), payResult);

        } catch (Exception e) {
            log.error("账务充值指令处理失败", e);
            CommandResult payResult = new CommandResult();
            payResult.setRealRtnFlag(true);
            payResult.setStatus(PayStatusEnums.FAIL.getCode());
            result.put(rechargeCommand.getCommandNo(), payResult);
        }
        return result;
    }

    /**
     * 执行取消方法
     *
     * @param commands 支付命令
     * @return map          指令执行结果
     */
    @Override
    public Map<String, CommandResult> doCancel(List<RechargeCommand> commands) {
        return null;
    }

}
